class UserController < BaseController
  # Be sure to include AuthenticationSystem in Application Controller instead
  
  # If you want "remember me" functionality, add this before_filter to Application Controller

  before_filter :login_required, :only => :setting
  before_filter :find_user, :only => ["setting", "update"]
  # say something nice, you goof!  something sweet.
#  def index
#    redirect_to(:action => 'signup') unless logged_in? || User.count > 0
#  end
  
  #【参数】
  #【返回值】
  #【注意】
  #【作者】 by ljl 2012.08.28
  def account
    @user = User.find_by_id(params[:id])
    if @user.blank?
      raise ActionController::UnknownAction
    else
      @boards = @user.boards
    end
  end
  
  #【参数】
  #【返回值】
  #【注意】
  #【作者】 by ljl 2012.08.28
  def pins
    if request.xhr?
      json = []
      user = User.find_by_id(params[:user_id])
      pins = Pin.find(:all, 
                      :include => :board,
                      :conditions => ["boards.user_id = ?", user.id],
                      :order => "pins.created_at desc, pins.id desc", 
                      :limit => APP_CONFIG[:page][:home_page],
                      :offset => (params[:page].to_i - 1) * APP_CONFIG[:page][:home_page]
      )
      pins.each do |pin|
        user = pin.user
        user_pin = UserPin.find_by_user_id_and_pin_id(session[:user], pin.id)
        p = {:id => pin.id,
             :board_id => pin.board.id,
             :board_title => pin.board.name,
             :user => {:id => user.id,
                       :name => user.name},
             :user_id => user.id,
             :media => pin.media,
             :description => pin.description,
             :link_count => pin.user_pins.count,
             :comment_count => pin.comments.count,
             :is_like  => true,
             :like_action => "",
             :like_btn => ""
        }
        if logged_in?
          p[:is_like] = current_user.id == user.id ? false : true 
        end
        if user_pin.present?
          p[:like_btn] = user_pin.like ? "取消喜欢" : "<em></em>喜欢"
          p[:like_action] = "unlike"
        else
          p[:like_btn] = "<em></em>喜欢"
          p[:like_action] = "like"
        end
        json << p
      end
      render :json => json.to_json
    else
      @user = User.find_by_id(params[:id])
      if @user.blank?
        raise ActionController::UnknownAction
      else
        @boards = @user.boards
      end
    end
  end
  
  #【参数】
  #【返回值】
  #【注意】
  #【作者】 by ljl 2012.08.28
  def likes
    if request.xhr?
      json = []
      user = User.find_by_id(params[:user_id])
      pins = Pin.find(:all, 
                      :include => :user_pins,
                      :conditions => ["user_pins.user_id = ?", user.id],
                      :order => "pins.created_at desc, pins.id desc", 
                      :limit => APP_CONFIG[:page][:home_page],
                      :offset => (params[:page].to_i - 1) * APP_CONFIG[:page][:home_page]
      )
      pins.each do |pin|
        user = pin.user
        user_pin = UserPin.find_by_user_id_and_pin_id(session[:user], pin.id)
        p = {:id => pin.id,
             :board_id => pin.board.id,
             :board_title => pin.board.name,
             :user => {:id => user.id,
                       :name => user.name},
             :user_id => user.id,
             :media => pin.media,
             :description => pin.description,
             :link_count => pin.user_pins.count,
             :comment_count => pin.comments.count,
             :is_like  => true,
             :like_action => "",
             :like_btn => ""
        }
        if logged_in?
          p[:is_like] = current_user.id == user.id ? false : true 
        end
        if user_pin.present?
          p[:like_btn] = user_pin.like ? "取消喜欢" : "<em></em>喜欢"
          p[:like_action] = "unlike"
        else
          p[:like_btn] = "<em></em>喜欢"
          p[:like_action] = "like"
        end
        json << p
      end
      render :json => json.to_json
    else
      @user = User.find_by_id(params[:id])
      if @user.blank?
        raise ActionController::UnknownAction
      else
        @boards = @user.boards
      end
    end
  end
  
  #【参数】
  #【返回值】
  #【注意】
  #【作者】 by ljl 2012.08.28
  def update
    @user.attributes = params[:user]
    if @user.valid?
      @user.save!
      flash[:notice] = t(:save_success)
      redirect_to :action => :setting
    else
      render :action => :setting
    end
  end
  
  def login
    return unless request.post?
    self.current_user = User.authenticate(params[:login], params[:password])
    params[:remember_me] = "1"
    if logged_in?
      if params[:remember_me] == "1"
        self.current_user.remember_me
        cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
      end
      redirect_back_or_default(:controller => '/')
      #      flash[:notice] = "Logged in successfully"
    end
  end
  
  def signup
    @user = User.new(params[:user])
    return unless request.post?
    @user.name = @user.login
    @user.save!
    self.current_user = @user
    redirect_back_or_default(:controller => '/follow', :action => 'index')
    #    flash[:notice] = "Thanks for signing up!"
  rescue ActiveRecord::RecordInvalid
    render :action => 'signup'
  end
  
  def logout
    self.current_user.forget_me if logged_in?
    cookies.delete :auth_token
    #    reset_session
    session[:user] = nil
    #    flash[:notice] = "You have been logged out."
    redirect_back_or_default(:controller => '/')
  end

  private
  
  def find_user
    @user = User.find_by_id(session[:user])
  end

end
